import request from '@/utils/request'

// 获取新闻列表
export const newsGetlist = (pages) => {
  return request.get('/news/list?pages=' + pages)
}

// 获取新闻栏目列表
export const newsGetType = (pages, newsType, size) => {
  return request.get(
    '/news/list?pages=' + pages + '&newsType=' + newsType + '&size=' + size
  )
}
// 获取新闻详情
export const newsGetDetail = (newsId) => {
  return request.get('/news/getOneById?id=' + newsId)
}

// 增加新闻
export const newsAdd = ({ title, userId, newsType, number, cover, newsContent }) => {
  return request.post(
    '/news/publish',
    { title, userId, newsType, number, cover, newsContent },
    {
      headers: { 'Content-Type': 'application/json' }
    }
  )
}

// 修改新闻
export const newsUpdate = ({
  newsId,
  title,
  userId,
  newsType,
  number,
  cover,
  publishTime,
  newsContent,
  author
}) => {
  return request.put(
    '/news/update?',
    {
      newsId,
      title,
      userId,
      newsType,
      number,
      cover,
      publishTime,
      newsContent,
      author
    },
    { headers: { 'Content-Type': 'application/json' } }
  )
}

// 删除新闻
export const newsDel = (newsId) => {
  return request.delete('/news/delete?newsId=' + newsId)
}

// 上传新闻图片
export const newPictures = (file) => {
  const headers = {
    Accept: '*/*',
    'Content-Type': 'multipart/form-data'
  }
  return request
    .post('/file/upload', file, {
      headers
    })
    .then((response) => {
      // 处理成功响应
      console.log(response.data)
      ElMessage.success('图片上传成功')
      return response.data
    })
    .catch((error) => {
      // 处理错误
      console.error('上传新闻图片失败:', error)
      // 如果有详细的错误信息，输出
      if (error.response && error.response.data) {
        console.error('详细错误信息:', error.response.data)
      }
      throw error
    })
}

//获取用户发布列表
export const userPublishList = (pages,size) => {
    return request.get('/news/userList?pages='+pages+'&size='+size)
}

// //获取用户发布列表
// export const userPublishList = (pages,size) => {
//     return request.get('/news/userList?pages='+pages+'&size='+size)
// }


